﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Data;
using DotNetCore.BannerManagement.Domain;
using System.Data.SqlClient; 
using System.Diagnostics;
using Microsoft.Practices.Repository;
using Microsoft.Practices.Repository.SQLServer;
using DotNetCore.BannerManagement.DAL.Interfaces;

namespace DotNetCore.BannerManagement.DAL.Domain.dnc_ZoneRepositoryArtifacts
{
    /// <summary>
    /// Repository that lets you find dnc_Zone in the database.
    /// </summary>
    public sealed class dnc_ZoneRepository : Repository<dnc_Zone>, Idnc_ZoneRepository 
    {
        public dnc_ZoneRepository(string databaseName)
            : base(databaseName)
        {
        }
        
        public dnc_ZoneRepository() : base()
        {
        }
        

		public List<dnc_Zone> GetAllFromdnc_Zone()
        {
            ISelectionFactory<NullableIdentity> selectionFactory = new GetAllFromdnc_ZoneSelectionFactory();
            
            try
            {
				NullableIdentity nullableIdentity = new NullableIdentity();
				return base.Find(selectionFactory, new GetAllFromdnc_ZoneFactory(), nullableIdentity);
			}
            catch (SqlException ex)
            {
                HandleSqlException(ex, selectionFactory);
            }
            
            return new List<dnc_Zone>();
        }

		public dnc_Zone Getdnc_ZoneByZoneImageId(System.Int32 zoneImageIdField)
        {
            ISelectionFactory<System.Int32> selectionFactory = new Getdnc_ZoneByZoneImageIdSelectionFactory();
            
            try
            {
				return base.FindOne(selectionFactory, new Getdnc_ZoneByZoneImageIdFactory(), zoneImageIdField);
			}
            catch (SqlException ex)
            {
                HandleSqlException(ex, selectionFactory);
            }
            
			return new dnc_Zone();
        }

		public dnc_Zone Getdnc_ZoneByID(System.Int32 iDField)
        {
            ISelectionFactory<System.Int32> selectionFactory = new Getdnc_ZoneByIDSelectionFactory();
            
            try
            {
				return base.FindOne(selectionFactory, new Getdnc_ZoneByIDFactory(), iDField);
			}
            catch (SqlException ex)
            {
                HandleSqlException(ex, selectionFactory);
            }
            
			return new dnc_Zone();
        }

        public void Add(dnc_Zone dnc_Zone)
        {
            dnc_ZoneInsertFactory insertFactory = new dnc_ZoneInsertFactory();
            try
            {
                base.Add(insertFactory, dnc_Zone);
            }
            catch (SqlException ex)
            {
                HandleSqlException(ex, insertFactory);
            }
        }

        public void Remove(System.Int32 iD)
        {
			IDeleteFactory<System.Int32> deleteFactory = new dnc_ZoneDeleteFactory();

            try
            {
				base.Remove(deleteFactory, iD);
            }
            catch (SqlException ex)
            {
                HandleSqlException(ex, deleteFactory);
            }            
        }
      

		public void Save(dnc_Zone dnc_Zone)
        {
            dnc_ZoneUpdateFactory updateFactory = new dnc_ZoneUpdateFactory();
            try
            {
                base.Save(updateFactory, dnc_Zone);
            }
            catch (SqlException ex)
            {
                HandleSqlException(ex, updateFactory);
            }
        }
        private void HandleSqlException(SqlException ex, IDbToBusinessEntityNameMapper mapper)
        {
            if(ex.Number == ErrorCodes.SqlUserRaisedError)
            {
                switch(ex.State)
                {
                    case ErrorCodes.ValidationError:
                        string[] messageParts = ex.Errors[0].Message.Split(':');
                        throw new RepositoryValidationException(
                            mapper.MapDbParameterToBusinessEntityProperty(messageParts[0]),
                            messageParts[1], ex);

                    case ErrorCodes.ConcurrencyViolationError:
                        throw new ConcurrencyViolationException(ex.Message, ex);

                }
            }

            throw new RepositoryFailureException(ex);
        }


    }
}

